using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{

    class SubjectProjectNewActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var user = this.Context.Token.Identity();
            if (request.IsMaster == false)
            {
                this.Prompt("只有管理员才可新建项目");
            }

            var Caption = this.AsyncDialog("Caption", d => new Web.UITextDialog
            {
                Title = "新建项目"
            });
            var team = new Project
            {
                Caption = Caption,
                ModifiedTime = DateTime.Now,
                AppKey = this.Context.AppKey ?? Guid.Empty,
                Id = UMC.Data.Utility.NewId(),
                user_id = user.Id,
                CreationTime = DateTime.Now,
                Sequence = 0,
                MemberTotal = 1
            };
            team.Code = UMC.Data.Utility.Parse36Encode(Utility.IntParse(team.Id.Value)[1]);
            DataFactory.Instance().Put(team);

            var p = new ProjectItem()
            {
                Id = UMC.Data.Utility.NewId(),
                Caption = "Home",
                Code = UMC.Data.Utility.Parse36Encode(Guid.NewGuid()),
                CreationTime = DateTime.Now,
                project_id = team.Id,
                Sequence = 0,
                user_id = user.Id,
            };
            DataFactory.Instance().Put(p);

            var portfolio = new Portfolio()
            {
                Id = UMC.Data.Utility.NewId(),
                Caption = "随笔",
                SubjectTotal = 0,
                CreationTime = DateTime.Now,
                Sequence = 0,
                user_id = user.Id,
                project_id = team.Id,
                project_item_id = p.Id,
            };
            DataFactory.Instance().Put(portfolio);

            DataFactory.Instance().Put(new ProjectDynamic
            {
                Time = UMC.Data.Utility.TimeSpan(),
                user_id = user.Id,
                Alias = user.Alias,
                Explain = "创建了项目",
                project_id = team.Id,
                refer_id = team.Id,
                Title = team.Caption,
                Type = DynamicType.Project
            });
            DataFactory.Instance().Put(new ProjectMember
            {
                Alias = user.Alias,
                user_id = user.Id.Value,
                AuthType = WebAuthType.Admin,
                CreationTime = DateTime.Now,
                project_id = team.Id

            });
            var ax = DataFactory.Instance().Attention(user.Id.Value) ?? new Attention { Id = user.Id };
            DataFactory.Instance().Put(new Attention
            {
                Id = user.Id,
                ProjectTotal = (ax.ProjectTotal ?? 0) + 1
            });
            this.Context.Send(new UIClick("System", "Image", new WebMeta("media_id", "https://res.apiumc.com/css/icon200.jpg", "Key", "Project").Put("id", team.Id)), false);
            this.Context.Send(new UMC.Web.WebMeta().Put("type", $"{request.Model}.Project").Put("id", team.Id).Put("text", team.Caption)
                         .Put("code", team.Code), true);
        }
    }
}